#pragma once
#include <cstdint>
#include <string>

// 博客概览表
#pragma db object
class Blog{
public:
    Blog(){}
    Blog(const std::string& title,const std::string& author_id,const std::string& date,
                const std::string& preview,const int32_t view_count,int32_t likes):
                title(title),author_id(author_id),date(date),preview(preview),view_count(view_count),likes(likes){}

#pragma db id auto
    unsigned long long id;

#pragma db type("VARCHAR(255)")
    std::string title;

#pragma db type("VARCHAR(255)")
    std::string author_id;

#pragma db type("VARCHAR(255)")
    std::string date;

#pragma db type("VARCHAR(255)")
    std::string preview;

#pragma db type("INT") default(0)
    int32_t view_count = 0;

#pragma db type("INT") default(0)
    int32_t likes = 0;

#pragma db type("TEXT")
    std::string content;

#pragma db type("INT") default(0)
    int32_t comment_total = 0;

#pragma db index type("TINYINT(1)") default(0)
    bool published = false;
};


// 文章评论表

#pragma db object
class Comment {
public:
    Comment() {}

    Comment(const unsigned long long& blog_id,
            const std::string& author_id,
            const std::string& content,
            int64_t created_at,
            const unsigned long long& parent_id)
        : blog_id(blog_id),
          author_id(author_id),
          content(content),
          created_at(created_at),
          parent_id(parent_id){}

// 数据库中的自增主键从1开始，所以默认parent_id为0表示顶级评论
#pragma db id auto
    unsigned long long id;

#pragma db index type("BIGINT UNSIGNED")
    unsigned long long blog_id;         // 文章id

// 这里将 author 只保存为 user_id，实际 user 信息由另一个表 UserStats 存储并通过联合查询获得
#pragma db type("VARCHAR(255)")
    std::string author_id;

#pragma db type("TEXT")
    std::string content;

#pragma db type("BIGINT")
    int64_t created_at;

#pragma db type("INT") default(0)
    int32_t likes = 0;

#pragma db type("BIGINT UNSIGNED")
    unsigned long long parent_id=0;

#pragma db type("INT") default(0)
    int32_t comment_total = 0;
};


// 定义联合主键类
#pragma db value
struct LikeKey {
    unsigned long long id;  // 博客id
    std::string email;      // 点赞者的email

    LikeKey() = default;
    LikeKey(unsigned long long id, const std::string& email)
        : id(id), email(email) {}

    bool operator==(const LikeKey& other) const {
        return id == other.id && email == other.email;
    }
};


// 博客点赞表
#pragma db object
class Like {
public:
    Like() = default;
    Like(const LikeKey& key, int64_t like_at)
        : key(key), like_at(like_at) {}

#pragma db id
    LikeKey key;

#pragma db type("BIGINT")
    int64_t like_at;
};
